package com.zsyh.jiujiu.utils;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.zsyh.jiujiu.R;


/**
 * Toast统一管理类
 *
 * @author way
 */
public class ToastUtils {
    // Toast
    private static Toast mTextToast;
    private static Toast mViewToast;

    /**
     * 短时间显示Toast
     *
     * @param context
     * @param message
     */
    public static void showShort(Context context, CharSequence message) {
        if (null == mTextToast) {
            mTextToast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
            // toast.setGravity(Gravity.CENTER, 0, 0);
        } else {
            mTextToast.setText(message);
        }
        mTextToast.show();
    }

    /**
     * 短时间显示Toast
     *
     * @param context
     * @param message
     */
    public static void showShort(Context context, int message) {
        if (null == mTextToast) {
            mTextToast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
            // toast.setGravity(Gravity.CENTER, 0, 0);
        } else {
            mTextToast.setText(message);
        }
        mTextToast.show();
    }

    /**
     * 长时间显示Toast
     *
     * @param context
     * @param message
     */
    public static void showLong(Context context, CharSequence message) {
        if (null == mTextToast) {
            mTextToast = Toast.makeText(context, message, Toast.LENGTH_LONG);
            // toast.setGravity(Gravity.CENTER, 0, 0);
        } else {
            mTextToast.setText(message);
        }
        mTextToast.show();
    }

    /**
     * 长时间显示Toast
     *
     * @param context
     * @param message
     */
    public static void showLong(Context context, int message) {
        if (null == mTextToast) {
            mTextToast = Toast.makeText(context, message, Toast.LENGTH_LONG);
            // toast.setGravity(Gravity.CENTER, 0, 0);
        } else {
            mTextToast.setText(message);
        }
        mTextToast.show();
    }

    /**
     * 自定义显示Toast时间
     *
     * @param context
     * @param message
     * @param duration
     */
    public static void show(Context context, CharSequence message, int duration) {
        if (null == mTextToast) {
            mTextToast = Toast.makeText(context, message, duration);
            // toast.setGravity(Gravity.CENTER, 0, 0);
        } else {
            mTextToast.setText(message);
        }
        mTextToast.show();
    }

    /**
     * 自定义显示Toast时间
     *
     * @param context
     * @param message
     * @param duration
     */
    public static void show(Context context, int message, int duration) {
        if (null == mTextToast) {
            mTextToast = Toast.makeText(context, message, duration);
            // toast.setGravity(Gravity.CENTER, 0, 0);
        } else {
            mTextToast.setText(message);
        }
        mTextToast.show();
    }

    /**
     * Hide the toast, if any.
     */
    public static void hideToast() {
        if (null != mTextToast) {
            mTextToast.cancel();
        }
        if (null != mViewToast) {
            mViewToast.cancel();
        }
    }

    public static void successToast(Context context, String msg) {
        mViewToast = new Toast(context);
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.toast_successs_view, null);
        TextView msgTv = (TextView) view.findViewById(R.id.tv_toast_content);
        msgTv.setText(msg);
        mViewToast.setView(view);
        mViewToast.setGravity(Gravity.CENTER, 0, 0);
        mViewToast.setDuration(Toast.LENGTH_SHORT);
        mViewToast.show();
    }
}
